package com.platform.common.config;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.result.ApiException;
import com.platform.common.result.ApiExceptionEnum;
import feign.Response;
import feign.Util;
import feign.codec.ErrorDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import java.io.IOException;

/**
 * @author GH
 */
@Slf4j
@Configuration
public class FeignClientErrorDecoder implements ErrorDecoder {
 
    @Override
    public Exception decode(String methodKey, Response response) {
        try {
            JSONObject jsonObject = JSONObject.parseObject(Util.toString(response.body().asReader()));
            return new ApiException(jsonObject.getIntValue("code"), jsonObject.getString("message"), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new ApiException(ApiExceptionEnum.UNKNOWN_ERROR);
    }

}